<template>
  <div style=" width: 80%;margin:40px auto;">
    <h3 style="font-size: 50px;text-align: center">办公室在线平台</h3>
  </div>
  <div class="login_box">
    <div style="font-size: 30px; text-align: center; padding: 30px 0; color: #333">欢迎登录</div>
    <el-form ref="form" :model="form" size="normal" :rules="rules">
      <el-form-item prop="loginId">
        <el-input prefix-icon="el-icon-user-solid" v-model="form.loginId" placeholder="请输入账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%" type="primary" @click="login">登 录</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" @click="$router.push('/register')">前往注册 >></el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      form: {
        loginId: '1001',
        password: '12345',
      },
      rules: {
        loginId: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
    }
  },
  mounted() {
    sessionStorage.removeItem("user")
  },
  methods: {
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/login", this.form).then(res => {
            if (res.code === '0') {
              this.$message.success("登录成功")
              sessionStorage.setItem("user", JSON.stringify(res.data))  // 缓存用户信息
              this.$router.push("/")  //登录成功之后进行页面的跳转，跳转到主页
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.login_box {
  width: 450px;
  margin: 90px auto;
  padding-left: 25px;
  padding-right: 25px;
  box-shadow: 0 5px 25px #cecece;
}
</style>
